<?php
declare (strict_types=1);

namespace app\admin\model\market\ticket;

use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 定义活动数据模型
 */
class TicketCoupon extends BaseModel
{
    use SoftDelete;
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'market_ticket_coupon';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    protected $type = [
        'ext_data'      => 'array',
        'derive_stime'  => 'timestamp',
        'derive_etime'  => 'timestamp',
        'maxprice'      => 'float',
        'price'         => 'float',
        'discount'      => 'float',
        'coupon_type'   => 'integer',
        'is_receive'    => 'integer',
        'ticket_id'     => 'integer',
        'mhm_id'        => 'integer',
        'effective_day' => 'integer'
    ];

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     *
     * @param $query
     */
    public function scopeMhm($query)
    {
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 新增事件
     *
     * @static
     * @param \think\Model $agreement
     * @return mixed|void
     */
    public static function onBeforeInsert($agreement)
    {
        // 机构
        $agreement->mhm_id = $agreement->getAttr('mhm_id') ?: request()->mhm_id;
    }
}
